<?php
/**
 * Class that operate on table 'role_modules'. Database Mysql.
 *
 * @author: http://phpdao.com
 * @date: 2015-05-26 14:36
 */
class RoleModulesMySqlDAO implements RoleModulesDAO{

	/**
	 * Get Domain object by primry key
	 *
	 * @param String $id primary key
	 * @return RoleModulesMySql 
	 */
	public function load($id){
		$sql = 'SELECT * FROM role_modules WHERE role_module_id = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($id);
		return $this->getRow($sqlQuery);
	}

	/**
	 * Get all records from table
	 */
	public function queryAll(){
		$sql = 'SELECT * FROM role_modules';
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	/**
	 * Get all records from table ordered by field
	 *
	 * @param $orderColumn column name
	 */
	public function queryAllOrderBy($orderColumn){
		$sql = 'SELECT * FROM role_modules ORDER BY '.$orderColumn;
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	/**
 	 * Delete record from table
 	 * @param roleModule primary key
 	 */
	public function delete($role_module_id){
		$sql = 'DELETE FROM role_modules WHERE role_module_id = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($role_module_id);
		return $this->executeUpdate($sqlQuery);
	}
	
	/**
 	 * Insert record to table
 	 *
 	 * @param RoleModulesMySql roleModule
 	 */
	public function insert($roleModule){
		$sql = 'INSERT INTO role_modules (role_id, module_id, created_date, updated_date, status) VALUES (?, ?, ?, ?, ?)';
		$sqlQuery = new SqlQuery($sql);
		
		$sqlQuery->setNumber($roleModule->roleId);
		$sqlQuery->setNumber($roleModule->moduleId);
		$sqlQuery->set($roleModule->created_date);
		$sqlQuery->set($roleModule->updated_date);
		$sqlQuery->setNumber($roleModule->status);

		$id = $this->executeInsert($sqlQuery);	
		$roleModule->roleModuleId = $id;
		return $id;
	}
	
	/**
 	 * Update record in table
 	 *
 	 * @param RoleModulesMySql roleModule
 	 */
	public function update($roleModule){
		$sql = 'UPDATE role_modules SET role_id = ?, module_id = ?, created_date = ?, updated_date = ?, status = ? WHERE role_module_id = ?';
		$sqlQuery = new SqlQuery($sql);
		
		$sqlQuery->setNumber($roleModule->roleId);
		$sqlQuery->setNumber($roleModule->moduleId);
		$sqlQuery->set($roleModule->created_date);
		$sqlQuery->set($roleModule->updated_date);
		$sqlQuery->setNumber($roleModule->status);

		$sqlQuery->setNumber($roleModule->roleModuleId);
		return $this->executeUpdate($sqlQuery);
	}

	/**
 	 * Delete all rows
 	 */
	public function clean(){
		$sql = 'DELETE FROM role_modules';
		$sqlQuery = new SqlQuery($sql);
		return $this->executeUpdate($sqlQuery);
	}

	public function queryByRoleId($value){
		$sql = 'SELECT * FROM role_modules WHERE role_id = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}

	public function queryByModuleId($value){
		$sql = 'SELECT * FROM role_modules WHERE module_id = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}

	public function queryByCreatedDate($value){
		$sql = 'SELECT * FROM role_modules WHERE created_date = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByUpdatedDate($value){
		$sql = 'SELECT * FROM role_modules WHERE updated_date = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByStatus($value){
		$sql = 'SELECT * FROM role_modules WHERE status = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}


	public function deleteByRoleId($value){
		$sql = 'DELETE FROM role_modules WHERE role_id = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByModuleId($value){
		$sql = 'DELETE FROM role_modules WHERE module_id = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByCreatedDate($value){
		$sql = 'DELETE FROM role_modules WHERE created_date = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByUpdatedDate($value){
		$sql = 'DELETE FROM role_modules WHERE updated_date = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByStatus($value){
		$sql = 'DELETE FROM role_modules WHERE status = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}


	
	/**
	 * Read row
	 *
	 * @return RoleModulesMySql 
	 */
	protected function readRow($row){
		$roleModule = new RoleModule();
		
		$roleModule->roleModuleId = $row['role_module_id'];
		$roleModule->roleId = $row['role_id'];
		$roleModule->moduleId = $row['module_id'];
		$roleModule->created_date = $row['created_date'];
		$roleModule->updated_date = $row['updated_date'];
		$roleModule->status = $row['status'];

		return $roleModule;
	}
	
	protected function getList($sqlQuery){
		$tab = QueryExecutor::execute($sqlQuery);
		$ret = array();
		for($i=0;$i<count($tab);$i++){
			$ret[$i] = $this->readRow($tab[$i]);
		}
		return $ret;
	}
	
	/**
	 * Get row
	 *
	 * @return RoleModulesMySql 
	 */
	protected function getRow($sqlQuery){
		$tab = QueryExecutor::execute($sqlQuery);
		if(count($tab)==0){
			return null;
		}
		return $this->readRow($tab[0]);		
	}
	
	/**
	 * Execute sql query
	 */
	protected function execute($sqlQuery){
		return QueryExecutor::execute($sqlQuery);
	}
	
		
	/**
	 * Execute sql query
	 */
	protected function executeUpdate($sqlQuery){
		return QueryExecutor::executeUpdate($sqlQuery);
	}

	/**
	 * Query for one row and one column
	 */
	protected function querySingleResult($sqlQuery){
		return QueryExecutor::queryForString($sqlQuery);
	}

	/**
	 * Insert row to table
	 */
	protected function executeInsert($sqlQuery){
		return QueryExecutor::executeInsert($sqlQuery);
	}
}
?>